local blaze = require "blaze"
local game = require "club.threecard.hbsl"

local M = game.ui.activity()

M.store_watchers = {
    gameplay = {
        ["$"] = blaze.noop,

        ["自己抢到红包"] = function(self, store, state, amount)
            self.packet_dispatcher:to_self(self.self_root, amount)
        end
    }
}

function M:ctor()
    self.res = "club.threecard.hbsl/ui/ingame.b:ingame"

    local phase_shower = game.require "ui.ingame.phase_shower"
    self.phase_shower = phase_shower.new()

    local hb_shower = game.require "ui.ingame.hb_shower"
    self.hb_shower = hb_shower.new()
    self.hb_shower:hide()

    local packet_dispatcher = game.require "ui.ingame.packet_dispatcher"
    self.packet_dispatcher = packet_dispatcher.new()

    local participant_shower = game.require "ui.ingame.participant_shower"
    self.participant_shower = participant_shower.new(self.packet_dispatcher)

    local player_info = game.gamelib.require "widget.player_info"
    self.player_info = player_info.new()

    local send_panel = game.require "ui.ingame.send_panel"
    self.send_panel = send_panel.new()
    self.send_panel:hide()

    local btn_operator = game.require "ui.ingame.btn_operator"
    self.btn_operator = btn_operator.new(self.send_panel)

    local self_record = game.require "ui.ingame.self_record"
    self.self_record = self_record.new()
    self.self_record:hide()

    local others_record = game.require "ui.ingame.others_record"
    self.others_record = others_record.new()
    self.others_record:hide()

    local resumer = game.require "ui.ingame.resumer"
    self.resumer = resumer.new()
end

function M:on_load()
    self:super("on_load")

    self.phase_shower:set_parent(self.phase_shower_root, false)
    self.phase_shower:load()

    self.hb_shower:set_parent(self.hb_shower_root, false)
    self.hb_shower:load()

    self.participant_shower:set_parent(self.participant_shower_root, false)
    self.participant_shower:load()

    self.btn_operator:set_parent(self.btn_operator_root, false)
    self.btn_operator:load()

    self.player_info:load()
    self.player_info:set_parent(self.self_root, false)

    self.send_panel:load()
    self.send_panel:set_parent(self.send_panel_root, false)

    self.packet_dispatcher:load()
    self.packet_dispatcher:set_parent(self.packet_dispatcher_root, false)

    self.self_record:load()
    self.self_record:set_parent(self.self_record_root, false)

    self.others_record:load()
    self.others_record:set_parent(self.others_record_root, false)

    self.resumer:load()
    self.resumer:set_parent(self.resumer_root, false)
end

function M.before_route_enter(to, from, next)
    game.store.gameplay.throttle(true)
    next()
end

function M:after_route_enter(to, from)
    self:super("after_route_enter", to, from)
    game.store.gameplay.throttle(false)
end

function M:on_injected(bind)
    bind("button.click", self.btn_exit, self.exit_ivk)
    bind("shortcut.escape", self.btn_exit.gameObject, self.exit_ivk)
    bind("button.click", self.btn_rule, self.rule_ivk)
    bind("button.click", self.btn_record, self.record_ivk)
    bind("button.click", self.btn_others, self.others_ivk)

    game.utility.button_style.highlight(self.btn_exit)
    game.utility.button_style.highlight(self.btn_rule)
    game.utility.button_style.highlight(self.btn_record)
    game.utility.button_style.highlight(self.btn_others)
end

function M:exit_ivk()
    game.audio.post("hbsl:click")
    game.game_scene.leave()
    game.router.go("/club.threecard.hbsl/main")
end

function M:rule_ivk()
    game.audio.post("hbsl:click")
end

function M:record_ivk()
    game.audio.post("hbsl:click")
    self.self_record:show()
end

function M:others_ivk()
    game.audio.post("hbsl:click")
    self.others_record:show()
end

function M:on_release()
    self:super("on_release")
    self.phase_shower:release()
    self.hb_shower:release()
    self.participant_shower:release()
    self.btn_operator:release()
    self.player_info:release()
    self.send_panel:release()
    self.packet_dispatcher:release()
    self.self_record:release()
end

return M